Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
Regex filter excludes ":" and includes at least one word character

The task is to exclude value if one contains :. The value also must include at least 1 word character.

matched string values are:

'   h-73 \r\n\t'
'   hey'
'hi  '
'7'

not matched values are:

'    \r\n\t' // neither letter not digit
'    ' // neither letter not digit
'  sds:  \r\n\t' // :

To exclude : I'd use /^[^:]+$/ to filter word characters I'd use /\w+/.

I have no idea how to use them together in the same regex because scope of each condition should be whole string.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The regex that matches your specifications is

/^[^\w:]*\w[^:]*$/

Details:

  • ^ - start of string
  • [^\w:]* - zero or more chars other than word and : chars
  • \w - one word char
  • [^:]* - any chars, zero or more occurrences, other than a colon
  • $ - end of string.

JavaScript demo:

const texts = ['   h-73 \r\n\t','   hey','hi  ','7','    \r\n\t','    ','  sds:  \r\n\t'];
const re  = /^[^\w:]*\w[^:]*$/;
for (const text of texts) {
    console.log("'"+text+"'", '=>', re.test(text));
}

NOTE: You could also use /^[^:]*\w[^:]*$/, but this regex grabs non-colons from the start of string as many times as possible, and then backtracks to find a word char, which is less efficient. So, the \w in the first negated character class is welcome.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda